package ycl.important.service.impl.region;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ycl.important.common.base.service.impl.BaseServiceImpl;
import ycl.important.entity.region.CarsAttribution;
import ycl.important.entity.region.City;
import ycl.important.entity.region.Province;
import ycl.important.mapper.region.CarsAttributionMapper;
import ycl.important.mapper.region.CityMapper;
import ycl.important.mapper.region.ProvinceMapper;
import ycl.important.service.region.CarsAttributionService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 车辆归属地 服务实现类
 * </p>
 *
 * @author YCL
 * @since 2021-06-07
 */
@Service("carsAttributionServiceImpl")
@Lazy
public class CarsAttributionServiceImpl
		extends BaseServiceImpl<CarsAttributionMapper, CarsAttribution>
		implements CarsAttributionService {

	@Resource
	private CarsAttributionMapper carsAttributionMapper;
	@Resource
	private CityMapper cityMapper;
	@Resource
	private ProvinceMapper provinceMapper;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void insertAll() {
		List<CarsAttribution> list = getCarsAttributions();
		saveBatch(list);
	}

	private List<CarsAttribution> getCarsAttributions() {
		List<CarsAttribution> list = new ArrayList<>();

		List<Province> provinces = provinceMapper.selectList(null);
		provinces.forEach(p -> {
			String provinceCode = p.getCode();
			String abbreviation = p.getAbbreviation();
			String provinceName = p.getName();

			LambdaQueryWrapper<City> cityWrapper = new LambdaQueryWrapper<>();
			cityWrapper.eq(City::getParentCode, provinceCode);

			if (abbreviation.equals("京") || abbreviation.equals("津") ||
					abbreviation.equals("沪") || abbreviation.equals("渝")||
					abbreviation.equals("港") || abbreviation.equals("澳")) {
				List<CarsAttribution> list1 = addSpecialCity(p, cityWrapper);
				list.addAll(list1);
				return;
			}

			List<City> cities = cityMapper.selectList(cityWrapper);
			cities.forEach(c -> {
				CarsAttribution e = new CarsAttribution(
						abbreviation,
						provinceCode,
						provinceName,
						c.getCode(),
						c.getName()
				);
				list.add(e);
			});
		});
		return list;
	}

	private List<CarsAttribution> addSpecialCity(Province p, LambdaQueryWrapper<City> cityWrapper) {
		List<CarsAttribution> list = new ArrayList<>();
		String provinceCode = p.getCode();
		String abbreviation = p.getAbbreviation();
		String provinceName = p.getName();

		if (abbreviation.equals("京") || abbreviation.equals("津") ||
				abbreviation.equals("沪") || abbreviation.equals("渝")) {
			//直辖市
			City city = cityMapper.selectOne(cityWrapper);

			if (abbreviation.equals("京") || abbreviation.equals("津")) {
				list.addAll(addSpecialCity(p, city, 16));
			} else if (abbreviation.equals("沪")) {
				list.addAll(addSpecialCity(p, city, 13));
			} else {
				list.addAll(addSpecialCity(p, city, 8));
			}
		} else {
			//港澳
			List<City> cities = cityMapper.selectList(cityWrapper);
			cities.forEach(c->{
				CarsAttribution e = new CarsAttribution(
						"粤",
						provinceCode,
						provinceName,
						c.getCode(),
						c.getName());
				e.setRemark(abbreviation);
				e.setCityAbbreviation("Z");
				list.add(e);
			});
		}

		return list;
	}

	private List<CarsAttribution> addSpecialCity(Province p, City c, int number) {
		ArrayList<CarsAttribution> list = new ArrayList<>();
		String provinceCode = p.getCode();
		String abbreviation = p.getAbbreviation();
		String provinceName = p.getName();
		String cityCode = c.getCode();
		String cityName = c.getName();
		for (int i = 0; i < number; i++) {
			CarsAttribution e = new CarsAttribution(
					abbreviation,
					provinceCode,
					provinceName,
					cityCode,
					cityName);
			list.add(e);
		}
		return list;
	}
}
